package com.pengcheng.taskapplication.net;

import android.content.Context;
import android.content.pm.ApplicationInfo;

/**
 * author: LJL.
 * date: 2017/4/7 15:18
 * desc  : Utils初始化相关
 */
public final class AbContextUtils {

    private static Context context;
    private static Boolean isDebug = null;

    public static Boolean isDebug() {
        return isDebug == null ? false : isDebug.booleanValue();
    }

    public static void syncIsDebug(Context context) {
        if (isDebug == null) {
            isDebug = context.getApplicationInfo() != null &&
                    (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        }
    }

    private AbContextUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    public static void init(Context context) {
        syncIsDebug(context);
        AbContextUtils.context = context.getApplicationContext();
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) {
            return context;
        }
        throw new NullPointerException("u should init first");
    }
}
